//
//  UINavigationController+CATransaction.swift
//  
//
//  Created by tangbl93 on 2021/1/6.
//

import UIKit

// Extension that provides completion blocks for push/pop on navigation controllers.
// Thanks to: http://stackoverflow.com/questions/9906966/completion-handler-for-uinavigationcontroller-pushviewcontrolleranimated
public extension UINavigationController {

    func yx_pushViewController(_ viewController: UIViewController, animated: Bool, completion: @escaping VoidClosure) {
        CATransaction.begin()
        CATransaction.setCompletionBlock(completion)
        pushViewController(viewController, animated: animated)
        CATransaction.commit()
    }

    func yx_popViewController(_ animated: Bool, completion: @escaping VoidClosure) {
        CATransaction.begin()
        CATransaction.setCompletionBlock(completion)
        self.popViewController(animated: animated)
        CATransaction.commit()
    }
    
    func yx_popToRootViewController(_ animated: Bool, completion: @escaping VoidClosure) {
        CATransaction.begin()
        CATransaction.setCompletionBlock(completion)
        self.popToRootViewController(animated: animated)
        CATransaction.commit()
    }
}

